﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;

using Invoicing.Model.Results;
using Invoicing.Model.Members;
using Invoicing.Logic.Members;
namespace Invoicing.Web.Management.Areas.Members.Controllers
{
    [RouteArea("Members")]
    [RoutePrefix("CustomerCategory")]
    [Authorize]
    public class CustomerCategoryApiController : ApiController
    {
        [Route("GetCategories")]
        [HttpPost]
        public JsonEntitySet<CustomerCategoryModel> GetCategories(JObject request)
        {
            string errinfo = "no data";
            try
            {
                CustomerCategoryQuery sea = new CustomerCategoryQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.Key = request.Value<string>("key");
                var result = CustomerCategoryLogic.GetCategories(sea);
                return new JsonEntitySet<CustomerCategoryModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<CustomerCategoryModel>(null, errinfo, false);
        }

        [Route("SaveCategory")]
        [HttpPost]
        public JsonEntity<CustomerCategoryModel> SaveCategory(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                CustomerCategoryModel model = new CustomerCategoryModel();
                model.CategoryName = request.Value<string>("categoryName");
                model.ID = request.Value<int>("categoryID");
                #endregion

                if (model.ID == 0)
                    model = CustomerCategoryLogic.CreateNew(model);
                else
                    CustomerCategoryLogic.Update(model);
                return new JsonEntity<CustomerCategoryModel>(model);
            }
            catch (Utils.InvoicingException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<CustomerCategoryModel>(null, errinfo, false);
        }

        [Route("DeleteCategory")]
        [HttpPost]
        public JsonResult<bool> DeleteClass(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var id = request.Value<int>("categoryID");
                #endregion
                CustomerCategoryLogic.Delete(id);
                return new JsonResult<bool>(true);
            }
            catch (Utils.InvoicingException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
